package com.jichangxiu.generator.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jichangxiu.common.annotation.Dynamic;
import com.jichangxiu.generator.entity.dto.query.QueryGenColumn;
import com.jichangxiu.generator.entity.po.GenColumn;
import com.jichangxiu.generator.entity.vo.GenColumnVo;
import com.jichangxiu.generator.mapper.GenColumnMapper;
import com.jichangxiu.generator.service.GenColumnService;
import com.jichangxiu.framework.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * GenColumnServiceImpl:
 *
 * @author JiChangXiu
 * @create 2024-06-30 13:31:02
 */
@Service
public class GenColumnServiceImpl extends BaseServiceImpl<GenColumnMapper, GenColumn, GenColumnVo> implements GenColumnService {

    /**
     * （可分页）查询生成列列表
     *
     * @param queryGenColumn 查询视图对象
     * @return （分页）生成列列表
     */
    @Dynamic
    @Override
    public Page<GenColumnVo> lists(QueryGenColumn queryGenColumn) {
        Page<GenColumnVo> genColumnVoPage = getBaseMapper().selectJoinPage(startPage(), GenColumnVo.class, buildQueryWrapper(queryGenColumn));
        if (Boolean.TRUE.equals(queryGenColumn.getIsExtend()))
            genColumnVoPage.setRecords(renderGenColumnVoList(genColumnVoPage.getRecords()));
        return genColumnVoPage;
    }

    /**
     * 渲染视图
     *
     * @param genColumnVoList 视图列表
     * @return 渲染后的视图列表
     */
    public List<GenColumnVo> renderGenColumnVoList(List<GenColumnVo> genColumnVoList) {
        if (ObjectUtil.isNotEmpty(genColumnVoList)) {
            // todo 扩展信息填入
        }
        return genColumnVoList;
    }

    /**
     * 构建查询条件
     *
     * @param queryGenColumn 查询用业务对象
     * @return 查询条件
     */
    public MPJLambdaWrapper<GenColumn> buildQueryWrapper(QueryGenColumn queryGenColumn) {
        MPJLambdaWrapper<GenColumn> lambdaQueryWrapper = new MPJLambdaWrapper<GenColumn>().selectAll(GenColumn.class);
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(queryGenColumn.getId()), GenColumn::getId, queryGenColumn.getId());
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(queryGenColumn.getRowId()), GenColumn::getRowId, queryGenColumn.getRowId());
        lambdaQueryWrapper.like(StrUtil.isNotEmpty(queryGenColumn.getName()), GenColumn::getName, queryGenColumn.getName());
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(queryGenColumn.getComment()), GenColumn::getComment, queryGenColumn.getComment());
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(queryGenColumn.getColumnType()), GenColumn::getColumnType, queryGenColumn.getColumnType());
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(queryGenColumn.getJavaType()), GenColumn::getJavaType, queryGenColumn.getJavaType());
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(queryGenColumn.getJavaField()), GenColumn::getJavaField, queryGenColumn.getJavaField());
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(queryGenColumn.getBigJavaField()), GenColumn::getBigJavaField, queryGenColumn.getBigJavaField());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(queryGenColumn.getPrimary()), GenColumn::getPrimary, queryGenColumn.getPrimary());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(queryGenColumn.getIncrement()), GenColumn::getIncrement, queryGenColumn.getIncrement());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(queryGenColumn.getRequired()), GenColumn::getRequired, queryGenColumn.getRequired());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(queryGenColumn.getUnique()), GenColumn::getUnique, queryGenColumn.getUnique());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(queryGenColumn.getInsert()), GenColumn::getInsert, queryGenColumn.getInsert());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(queryGenColumn.getEdit()), GenColumn::getEdit, queryGenColumn.getEdit());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(queryGenColumn.getList()), GenColumn::getList, queryGenColumn.getList());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(queryGenColumn.getQuery()), GenColumn::getQuery, queryGenColumn.getQuery());
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(queryGenColumn.getQueryType()), GenColumn::getQueryType, queryGenColumn.getQueryType());
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(queryGenColumn.getHtmlType()), GenColumn::getHtmlType, queryGenColumn.getHtmlType());
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(queryGenColumn.getLayoutType()), GenColumn::getLayoutType, queryGenColumn.getLayoutType());
        lambdaQueryWrapper.like(StrUtil.isNotEmpty(queryGenColumn.getDictCode()), GenColumn::getDictCode, queryGenColumn.getDictCode());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(queryGenColumn.getSort()), GenColumn::getSort, queryGenColumn.getSort());
        lambdaQueryWrapper.like(StrUtil.isNotEmpty(queryGenColumn.getRemark()), GenColumn::getRemark, queryGenColumn.getRemark());
        return lambdaQueryWrapper;
    }

}